/* eslint-disable */

// 由于ie11的cookie默认不能跨目录访问，要设置path为/
const COOKIE_PATH = '/'

const Cookie = {
  //设置
  set : function(name, value, str) {
    var exp = "";
    if (str) {
      //如果为数字
      exp = new Date();
      exp.setTime(exp.getTime()+getMillisec(str));
      exp = ';expires=' + exp.toGMTString();
    }
    document.cookie = name + "=" + escape(value) + exp + (typeof COOKIE_PATH !== 'undefined' ? ';path=/' : '');
  },
  //读取
  get : function(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if ( arr = document.cookie.match(reg)) {
      return unescape(arr[2]);
    } else {
      return null;
    }
  },
  //删除
  del : function(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var val = this.get(name);
    if (val != null)
      document.cookie = name + "=" + val + ";expires=" + exp.toGMTString();
  },
  //何时到期，str:d10为10天后，h10为10为小时后
  exp : function(name, str) {
    var exp = new Date();
    exp.setTime(exp.getTime()+this.getMillisec(str));
    var val = this.get(name);
    if (val != null)
      document.cookie = name + "=" + val + ";expires=" + exp.toGMTString();
  },
  getMillisec : function(str) {
    var sign = str.slice(0, 1);
    var value = str.slice(1);
    if (sign == "s") {
      return value * 1000;
    } else if (sign == "m") {
      return value * 60 * 1000;
    } else if (sign == "h") {
      return value * 60 * 60 * 1000;
    } else if (sign == "d") {
      return value * 24 * 60 * 60 * 1000;
    } else {
      return value;
    }
  }
}

export default Cookie
